package com.cloudcc.mobile.parser;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.entity.login.LoginUser;
import com.cloudcc.mobile.entity.login.LoginUserInfo;
import com.cloudcc.mobile.util.Json;
import com.cloudcc.mobile.util.Tools;

public class LoginParser {
	public static LoginUser getLoginUser(String jsonString, Context context) {
		LoginUser loginuser = new LoginUser();
		try {
			JSONObject myjson = new JSONObject(jsonString);
			loginuser.setResult(myjson.getBoolean("result"));
			if (!myjson.getBoolean("result")) {
				loginuser.setReturnCode(myjson.getInt("returnCode"));
				loginuser.setReturnInfo(myjson.getString("returnInfo"));
			} else {
				loginuser.setBinding(myjson.getString("binding"));
				loginuser.setReturnCode(myjson.getInt("returnCode"));
				loginuser.setReturnInfo(myjson.getString("returnInfo"));
				AppContext.binding = myjson.getString("binding");
				JSONObject jsonObj = myjson.getJSONObject("userInfo");
				LoginUserInfo userinfo = (LoginUserInfo) Json.toObject(
						jsonObj.toString(), LoginUserInfo.class);
				// MyApplication.userId=userinfo.getUserId();
				// MyApplication.orgId=userinfo.getOrgId();
				// MyApplication.orgName=userinfo.getOrgName();
				// MyApplication.userName=userinfo.getUserName();
				// MyApplication.jiaose=userinfo.getRoleName();
				// MyApplication.useremail=userinfo.getEmail();
				// MyApplication.zuzhiname=userinfo.getOrgName();
				// MyApplication.jiandangname=userinfo.getProfileName();
				Tools.i("binding", AppContext.binding);
				loginuser.setLoginUserInfo(userinfo);
			}
		} catch (JSONException e) {
			Tools.handle(e);
		}
		return loginuser;
	}

	/**
	 * 获取小网页网址
	 */
	public static String getAppURl(String jsonString) {
		String url = "";
		try {
			JSONObject myjson = new JSONObject(jsonString);
			boolean result = myjson.getBoolean("result");
			if (!result) {
				return "";
			}
			JSONObject jsonObj = myjson.getJSONObject("data");
			url = jsonObj.getString("url");
			return url;
		} catch (JSONException e) {
			Tools.handle(e);
		}
		return "";
	}
}
